/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.business.dao.CarDriverArchivesMapper;
import com.rf.business.model.CarDriverArchives;
import com.rf.business.model.CarDriverArchivesExample;
import com.rf.business.service.CarDriverArchivesService;

@Service("carDriverArchivesService")
public class CarDriverArchivesServiceImpl implements CarDriverArchivesService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private CarDriverArchivesMapper carDriverArchivesMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param carDriverArchivesId
	 * @return CarDriverArchives
	 */
   	@Override
	public CarDriverArchives getCarDriverArchivesByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return carDriverArchivesMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据CarDriverArchives获取数据
	 * @param CarDriverArchives
	 * @return List<CarDriverArchives>
	 */
	@Override
	public List<CarDriverArchives> getCarDriverArchives(CarDriverArchives carDriverArchives) throws Exception{
		// TODO Auto-generated method stub
		CarDriverArchivesExample example = new CarDriverArchivesExample();
		com.rf.business.model.CarDriverArchivesExample.Criteria criteria= example.createCriteria();
		
		if(carDriverArchives.getCaId()!=null && !carDriverArchives.getCaId().equals("")){
			criteria.andCaIdEqualTo(carDriverArchives.getCaId());
		}
		if(carDriverArchives.getArchiveNo()!=null && !carDriverArchives.getArchiveNo().equals("")){
			criteria.andArchiveNoEqualTo(carDriverArchives.getArchiveNo());
		}
		if(carDriverArchives.getPassportNo() != null && !carDriverArchives.getPassportNo().equals("")){
			criteria.andPassportNoEqualTo(carDriverArchives.getPassportNo());
		}
		
		List<CarDriverArchives> list = carDriverArchivesMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<CarDriverArchives>
	 */
	@Override
	public List<CarDriverArchives> getCarDriverArchives() throws Exception{
		// TODO Auto-generated method stub
		CarDriverArchivesExample example = new CarDriverArchivesExample();
		com.rf.business.model.CarDriverArchivesExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return carDriverArchivesMapper.selectByExample(example);
	}
   	
	
	/**
	 * 根据车辆备案号获取驾驶员信息
	 * @param archiveNo
	 * @return List<CarDriverArchives>
	 */
	@Override
	public List<CarDriverArchives> getCarDriverArchivesByArchiveNo(String archiveNo) throws Exception{
		// TODO Auto-generated method stub
		List<CarDriverArchives> cdaList = new ArrayList<CarDriverArchives>();
		String sql = "select c.* from BUS_CAR_ARCHIVES t,bus_car_driver_archives c "
				+ " where t.id = c.ca_id and t.archive_no='"+archiveNo+"'";
		List<Map<String, Object>> list = sqlAdapterMapper.selectSQL(sql);
		for(Map map:list){
			CarDriverArchives cda = new CarDriverArchives();
			cda.setId((String)map.get("ID"));
			cda.setName((String)map.get("NAME"));
			cdaList.add(cda);
		}
		return cdaList;
	}
	
	/**
	 * 保存信息
	 * @param CarDriverArchives
	 * @return
	 */
	@Override
	public void addCarDriverArchives(CarDriverArchives carDriverArchives) throws Exception{
		// TODO Auto-generated method stub
		carDriverArchivesMapper.insert(carDriverArchives);
	}

	/**
	 * 修改数据
	 * @param CarDriverArchives
	 * @param
	 */
	@Override
	public void updateCarDriverArchives(CarDriverArchives carDriverArchives) throws Exception{
		// TODO Auto-generated method stub
		carDriverArchivesMapper.updateByPrimaryKeySelective(carDriverArchives);
	}
	
	/**
	 * 删除数据
	 * @param carDriverArchivesId
	 * @param
	 */
	@Override
	public void deleteCarDriverArchives(String id) throws Exception{
		// TODO Auto-generated method stub
		carDriverArchivesMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取数据分页
	 * @param CarDriverArchives
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getCarDriverArchivesPage(CarDriverArchives carDriverArchives,PageUtils pageUtils) throws Exception{
		CarDriverArchivesExample example = new CarDriverArchivesExample();
		com.rf.business.model.CarDriverArchivesExample.Criteria criteria= example.createCriteria();
		if(carDriverArchives.getCaId()!=null){
			criteria.andCaIdEqualTo(carDriverArchives.getCaId());
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<CarDriverArchives> list = carDriverArchivesMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

